package com.yyi.springcloud.controller;

import com.github.pagehelper.PageInfo;

import com.yyi.springcloud.common.JedisClientSingle;
import com.yyi.springcloud.common.PageInfoResult;
import com.yyi.springcloud.entities.*;
import com.yyi.springcloud.service.Local_SensorInfoService;
import com.yyi.springcloud.service.Local_TestDataService;
import com.yyi.springcloud.service.Local_TestProdInfoValueService;
import com.yyi.springcloud.utils.ExportExcelUtils_DataList_Mysql;
import com.yyi.springcloud.utils.JsonUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;

@Controller
@RequestMapping("/mysqlapi/TestData")
public class Mysql_TestDataController {
	//按类型注入,不适合多继承
	@Autowired
	private Local_TestDataService testDataService;
	@Autowired
	private Local_TestProdInfoValueService testProdInfoValueService;
	@Autowired
	private Local_SensorInfoService sensorInfoService;
	// redis客户端
	@Resource
	private JedisClientSingle jedisClientSingle;
	// 数据列表分页的相关信息
	private int total;
	private int pages;
	// 开停点数据分页的相关信息
	private int startAndStopTotal;
	private int startAndStopPages;

	/**
	 * 1.查询数据列表
	 * 
	 * @param request
	 * @param model
	 * @param response
	 * @return
	 */
	// http://localhost:8001/mysqlapi/TestData/getAllTestDataVo?labcode=Haierrefrigerator20151214&testunitno=1
	@ResponseBody
	@RequestMapping(value = "/getAllTestDataVo", method = RequestMethod.GET)
	public PageInfoResult<TestDataVo> getAllTestDataVo(HttpServletRequest request, Model model,
                                                       HttpServletResponse response) {
		PageInfoResult<TestDataVo> testDataVoPage = new PageInfoResult<TestDataVo>();
		List<TestDataVo> testDataVoList = new ArrayList<TestDataVo>();
		String labcode = request.getParameter("labcode");
		String testunitno = request.getParameter("testunitno");
		String startdate = request.getParameter("startdate");// 开始时间
		String enddate = request.getParameter("enddate");// 结束时间
		int pageNum = Integer.parseInt(request.getParameter("pageNum") == null ? "1" : request.getParameter("pageNum"));
		int pageSize = Integer
				.parseInt(request.getParameter("pageSize") == null ? "15" : request.getParameter("pageSize"));
		int testunit = (testunitno == null ? -1 : Integer.parseInt(testunitno));
		// 从redis缓存中获取数据
		testDataVoList = getTestDataFromRedis(labcode, testunit, startdate, enddate, pageNum, pageSize);
		// 设置分页的相关信息
		testDataVoPage.setList(testDataVoList);
		testDataVoPage.setPageNum(pageNum);
		testDataVoPage.setPageSize(pageSize);
		testDataVoPage.setTotal(total);
		testDataVoPage.setPages(pages);
		// 将结果集进行筛选

		return testDataVoPage;
	}

	/**
	 * 2.根据primarykey查询被使用的传感器信息
	 * 
	 * @param primaryKey
	 * @return
	 */
	// http://localhost:8001/mysqlapi/TestData/getAllSelectSensorName?primaryKey=2017-11-1611:17:211&testunitno=1
	@ResponseBody
	@RequestMapping(value = "/getAllSelectSensorName", method = RequestMethod.GET)
	public List<SensorInfoModel> getAllSelectSensorName(String primaryKey) {
		SensorInfoModel SensorInfoModel = new SensorInfoModel();
		SensorInfoModel.setPrimarykey(primaryKey);
		SensorInfoModel.setSelected(1);// 查找被选择的传感器
		return sensorInfoService.getSensorInfoByCondition(SensorInfoModel);
	}

	/**
	 * 3.查询历史测试数据
	 * 
	 * @param request
	 * @param model
	 * @param response
	 * @return
	 */
	// http://localhost:8080/bxxs/mysqlapi/TestData/getHistoryTestData?labcode=Haierrefrigerator20151214&testunitno=6
	@ResponseBody
	@RequestMapping(value = "/getHistoryTestData", method = RequestMethod.GET)
	public PageInfoResult<TestDataVo> getHistoryTestData(HttpServletRequest request, Model model,
			HttpServletResponse response) {
		PageInfoResult<TestDataVo> testDataVoPage = new PageInfoResult<TestDataVo>();
		List<TestDataVo> testDataVoList = new ArrayList<TestDataVo>();
		String labcode = request.getParameter("labcode");
		String testunitno = request.getParameter("testunitno");
		int pageNum = Integer.parseInt(request.getParameter("pageNum") == null ? "1" : request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize") == null ? "10" : request.getParameter("pageSize"));

		// 1.根据实验室编码,台位号查看台位的开测时间
		TestProdInfoValueModel testProdInfoValueQuery = new TestProdInfoValueModel();
		testProdInfoValueQuery.setLabcode(labcode);
		testProdInfoValueQuery.setTestunitno(Integer.parseInt(testunitno));
		TestProdInfoValueModel testProdInfoValue = testProdInfoValueService
				.getTestProdInfoValueByCondition(testProdInfoValueQuery);

		// 2.根据台位的开测时间查看测试数据
		PageInfo<TestDataModel> testDataPage = testDataService.getTestDataByCondition(testProdInfoValue.getPrimarykey(),
				null, null, pageNum, pageSize);
		for (int i = 0; i < testDataPage.getList().size(); i++) {
			TestDataVo testDataVo = TestDataToTestDataVo(testDataPage.getList().get(i));
			testDataVoList.add(testDataVo);
		}
		// 设置分页的相关信息
		testDataVoPage.setList(testDataVoList);
		testDataVoPage.setPageNum(pageNum);
		testDataVoPage.setPageSize(pageSize);
		testDataVoPage.setTotal((int) testDataPage.getTotal());
		testDataVoPage.setPages(testDataPage.getPages());
		return testDataVoPage;
	}

	/**
	 * 4.根据台位号和传感器编号查询该传感器的所有测试数据(跟方法5类似,暂时未用到)
	 * 
	 * @param labcode
	 * @param testunitno
	 * @param sensorno
	 * @return
	 */
	// http://localhost:8080/bxxs/mysqlapi/TestData/getSensorAllNowTestData?labcode=Haierrefrigerator20151214&testunitno=1&sensorno=2
	@ResponseBody
	@RequestMapping(value = "/getSensorAllNowTestData", method = RequestMethod.GET)
	public List<CurveVo> getSensorAllNowTestData(String labcode, String testunitno, String sensorno) {
		List<CurveVo> sensorAllNowTestData = testDataService.getSensorAllNowTestData(labcode, testunitno, sensorno);
		return sensorAllNowTestData;
	}

	/**
	 * 5.根据primaryKey和传感器编号查询该传感器的所有测试数据(跟方法4类似)
	 * 
	 * @param labcode
	 * @param primaryKey
	 * @param sensorno
	 * @return
	 */
	// http://localhost:8080/bxxs/mysqlapi/TestData/getSensorTestDataNow?labcode=Haierrefrigerator20151214&primaryKey=2017-11-1611:17:211&sensorno=1
	@ResponseBody
	@RequestMapping(value = "/getSensorTestDataNow", method = RequestMethod.GET)
	public List<CurveVo> getSensorTestDataNow(String labcode, String primaryKey, String sensorno) {
		List<CurveVo> sensorAllNowTestData = new ArrayList<CurveVo>();
		// 将该传感器所有的测试数据保存到redis中
		String redisKey = labcode + "-" + primaryKey + "-" + sensorno + "info";
		try {
			String result = jedisClientSingle.get(redisKey);
			if (result != "" && result != null) {
				return JsonUtil.jsonToList(result, CurveVo.class);
			} else {
				sensorAllNowTestData = testDataService.getSensorTestDataNow(labcode, primaryKey, sensorno);
				jedisClientSingle.set(redisKey, JsonUtil.list2json(sensorAllNowTestData));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sensorAllNowTestData;
	}

	/**
	 * 6.根据primaryKey查询传感器的最新一条测试数据
	 * 
	 * @param primaryKey
	 * @param sensorNo
	 * @return
	 */
	// http://localhost:8080/bxxs/mysqlapi/TestData/getNewOneTestData?primaryKey=2017-11-1611:17:211
	@ResponseBody
	@RequestMapping(value = "/getNewOneTestData", method = RequestMethod.GET)
	public List<TestDataDto> getNewOneTestData(String primaryKey, String sensorNo) {
		return testDataService.getNewOneTestData(primaryKey, sensorNo);
	}

	/**
	 * 7.查询数据列表中最新10条测试数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	// http://localhost:8080/bxxs/mysqlapi/TestData/getTestDataVoNewTop10?labcode=Haierrefrigerator20151214&testunitno=6
	@ResponseBody
	@RequestMapping(value = "/getTestDataVoNewTop10", method = RequestMethod.GET)
	public List<TestDataVo> getTestDataVoNewTop10(HttpServletRequest request, HttpServletResponse response) {
		List<TestDataVo> testDataVoList = new ArrayList<TestDataVo>();
		String labcode = request.getParameter("labcode");
		String testunitno = request.getParameter("testunitno");
		int testunit = (testunitno == null ? -1 : Integer.parseInt(testunitno));
		// 从redis缓存中获取数据(最新10条测试数据)
		testDataVoList = getTestDataNewTop10(labcode, testunit);
		return testDataVoList;
	}

	/**
	 *
	 * 8.根据x轴的值查询y轴的值
	 * 
	 * @param labcode
	 * @param primaryKey
	 * @param sensorNo
	 * @param xAxisValue
	 * @return
	 */
	// http://localhost:8080/bxxs/mysqlapi/TestData/getYAxisValue?labcode=Haierrefrigerator20151214&primaryKey=2017-11-1611:17:211&sensorno=1&xAxisValue=1
	@ResponseBody
	@RequestMapping(value = "/getYAxisValue", method = RequestMethod.GET)
	public String getYAxisValue(@RequestParam(required = true) String labcode,
                                @RequestParam(required = true) String primaryKey, @RequestParam(required = true) String sensorNo,
                                @RequestParam(required = true) String xAxisValue) {
		CurveVo curveVo = new CurveVo();
		// 从redis中获取某传感器所有的测试数据
		String redisKey = labcode + "-" + primaryKey + "-" + sensorNo + "info";
		try {
			String result = jedisClientSingle.get(redisKey);
			if (result != "" && result != null) {
				curveVo = JsonUtil.jsonToList(result, CurveVo.class).get(0);
			} else {
				return "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 1.获取所有的相对时间,并根据X轴值获取相对时间的下标
		List<String> relativetimeList = curveVo.getRelativetime();
		int lastIndex = relativetimeList.lastIndexOf(xAxisValue);
		if (lastIndex != -1) {
			return String.valueOf(curveVo.getSensorvalues().get(lastIndex));
		} else {
			return "";
		}
	}

	/**
	 * 9.查询某个传感器所有x轴和y轴值
	 * 
	 * @return
	 */
	// http://localhost:8080/bxxs/mysqlapi/TestData/getAllAxisValue?labcode=Haierrefrigerator20151214&primaryKey=2017-11-1611:17:211&sensorno=1
	@ResponseBody
	@RequestMapping(value = "/getAllAxisValue", method = RequestMethod.GET)
	public List<SensorInfoDto> getAllAxisValue(@RequestParam(required = true) String labcode,
                                               @RequestParam(required = true) String primaryKey, @RequestParam(required = true) String sensorno) {
		CurveVo curveVo = new CurveVo();
		// 从redis中获取某传感器所有的测试数据
		String redisKey = labcode + "-" + primaryKey + "-" + sensorno + "info";
		try {
			String result = jedisClientSingle.get(redisKey);
			if (result != "" && result != null) {
				curveVo = JsonUtil.jsonToList(result, CurveVo.class).get(0);
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<SensorInfoDto> SensorInfoDtoList = new ArrayList<SensorInfoDto>();
		// 1.获取所有的相对时间,并根据X轴值获取相对时间的下标
		List<String> relativetimeList = curveVo.getRelativetime();
		for (int i = 0; i < relativetimeList.size(); i++) {
			SensorInfoDto sensorInfoDto = new SensorInfoDto();
			sensorInfoDto.setSensorno(curveVo.getSensorno());
			sensorInfoDto.setxAxisValue(Double.parseDouble(relativetimeList.get(i)));
			sensorInfoDto.setyAxisValue(curveVo.getSensorvalues().get(i));
			SensorInfoDtoList.add(sensorInfoDto);
		}
		return SensorInfoDtoList;

	}

	/**
	 * 10.开停点列表
	 * 
	 * @param request
	 * @param model
	 * @param response
	 * @return
	 */
	// http://localhost:8080/bxxs/mysqlapi/getAllStartAndStopData/getAllStartAndStopData?labcode=Haierrefrigerator20151214&testunitno=6
	@ResponseBody
	@RequestMapping(value = "/getAllStartAndStopData", method = RequestMethod.GET)
	public PageInfoResult<TestDataVo> getAllStartAndStopData(HttpServletRequest request, Model model,
			HttpServletResponse response) {
		PageInfoResult<TestDataVo> testDataVoPage = new PageInfoResult<TestDataVo>();
		List<TestDataVo> testDataVoList = new ArrayList<TestDataVo>();
		String labcode = request.getParameter("labcode");
		String testunitno = request.getParameter("testunitno");
		String startdate = request.getParameter("startdate");// 开始时间
		String enddate = request.getParameter("enddate");// 结束时间
		int pageNum = Integer.parseInt(request.getParameter("pageNum") == null ? "1" : request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize") == null ? "10" : request.getParameter("pageSize"));
		int testunit = (testunitno == null ? -1 : Integer.parseInt(testunitno));
		// 从redis缓存中获取数据
		testDataVoList = getStartAndStopDataFromRedis(labcode, testunit, startdate, enddate, pageNum, pageSize);
		// 设置分页的相关信息
		testDataVoPage.setList(testDataVoList);
		testDataVoPage.setPageNum(pageNum);
		testDataVoPage.setPageSize(pageSize);
		testDataVoPage.setTotal(startAndStopTotal);
		testDataVoPage.setPages(startAndStopPages);
		// 将结果集进行筛选

		return testDataVoPage;
	}

	/**
	 * 11.导出数据列表
     * method = { RequestMethod.GET, RequestMethod.POST }
	*/
	@RequestMapping(value = "exportTestData", method = RequestMethod.GET)
	public OutputStream exportTestData(HttpServletResponse response, String labCode, int testUnit, String startDate, String endDate) {
        OutputStream outputStream = null;
		List<TestDataVo> testDataVoList = listDatas2Excel(labCode, testUnit, startDate, endDate);
		System.out.println(testDataVoList.size());
		// 开始导出数据
		try {
		    mysql(response, testDataVoList);
		} catch (Exception e) {
			e.printStackTrace();
		}
      return outputStream;
	}

	/**
	 * 12.根据primaryKey,传感器分组编号,开始时间和结束时间等查询并统计传感器信息(统计值专用)
	 * 
	 * @param primaryKey
	 * @param groupNo
	 * @return
	 */
	// http://localhost:8080/bxxs/mysqlapi/TestData/getStatisticsSensorInfoByCondition?primaryKey=2017-11-2109:10:226&groupNo=1
    //method = { RequestMethod.GET, RequestMethod.POST }
    @ResponseBody
	@RequestMapping(value = "/getStatisticsSensorInfoByCondition", method = RequestMethod.GET)
	public List<TestDataStatisticsVo> getStatisticsSensorInfoByCondition(String primaryKey, String groupNo,
			String startTime, String endTime) {
		try {
			primaryKey = new String(primaryKey.getBytes("iso-8859-1"), "utf-8");
			groupNo = new String(groupNo.getBytes("iso-8859-1"), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return testDataService.getStatisticsSensorInfoByCondition(primaryKey, groupNo, startTime, endTime);
	}

	/**
	 * 描述：通过 jquery.form.js 插件提供的ajax方式导出Excel
	 */
	public void mysql(HttpServletResponse response, List<TestDataVo> list) throws Exception {
		System.out.println("开始导出excel文件！");
		OutputStream os = null;
		Workbook wb = null; // 工作薄
		try {
			// 导出Excel文件数据
			ExportExcelUtils_DataList_Mysql util = new ExportExcelUtils_DataList_Mysql();
			File file = util.getExcelDemoFile("ExcelDemoFile/dataListTemplet.xlsx");
			String sheetName = "Sheet1";
            wb = util.writeNewExcel(file, sheetName, list);
			String fileName = "数据列表.xlsx";
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
			os = response.getOutputStream();
			wb.write(os);
			System.out.println("文件保存成功");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			os.flush();
			os.close();
			wb.close();
		}
	}

	/**
	 * 将TestDataModel转换为TestDataVo(数据列表专用)
	 * 
	 * @param TestData
	 * @return
	 */
	public TestDataVo TestDataToTestDataVo(TestDataModel TestData) {
		List<String> sensorNoList = new ArrayList<String>();// 传感器编号集合
		List<String> sensorNameList = new ArrayList<String>();// 传感器名称集合
		List<String> sensorValueList = new ArrayList<String>();// 传感器数据集合
		TestDataVo testDataVo = new TestDataVo();
		try {
			BeanUtils.copyProperties(testDataVo, TestData);
			// 切割传感器数据
			String[] split = TestData.getSensorvalue().split("@");
			for (int i = 0; i < split.length; i++) {
				if (!split[i].isEmpty()) {
					String[] sensorInfo = split[i].split(":");
					// 根据传感器编号查询传感器名称
					String sensorName = getSensorNameBySensorNo(TestData.getPrimarykey(), sensorInfo[0]);
					if (sensorName != null) {
						// 对传感器名称和数据进行格式化
						//sensorInfo[1] = sensorValueFormat(sensorName, sensorInfo[1]);
						sensorName = sensorNameFormat(sensorName);
						sensorNoList.add(sensorInfo[0]);// 添加传感器编号
						sensorValueList.add(sensorInfo[1]);// 添加传感器数据
						sensorNameList.add(sensorName);// 添加传感器名称
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//testDataVo.setDate(TestData.getPrimarykey().substring(0, 10));
		String acTime = TestData.getAcqusitiontime();
		String times[] = acTime.split(" ");
		testDataVo.setDate(times[0]);
		testDataVo.setTime(times[1]);
		/*// 计算出当前数据的时间
		StringBuilder sb = new StringBuilder(TestData.getPrimarykey().replaceAll(" ",""));
		DateTime dt = new DateTime(sb.insert(10, "T").substring(0, 19));
		double seconds = TestData.getHowlong() * 3600;
		// 四舍五入,保留0位小数
		String formatSeconds = new Formatter().format("%.0f", seconds).toString();
		dt = dt.plusSeconds(Integer.parseInt(formatSeconds));

		String nowDataTime = TestData.getPrimarykey().substring(10, 18) + TestData.getHowlong();
		testDataVo.setTime(dt.toString("HH:mm:ss"));*/
		testDataVo.setTestunitno(Integer.parseInt(TestData.getPrimarykey().substring(17)));
		testDataVo.setSensorNoList(sensorNoList);
		testDataVo.setSensorValueList(sensorValueList);
		testDataVo.setSensorNameList(sensorNameList);
		return testDataVo;
	}

	/**
	 * 将TestDataModel转换为TestDataVo(开停点数据专用)
	 * 
	 * @param TestData
	 * @return
	 */
	public TestDataVo StartAndStopDataToTestDataVo(TestDataModel TestData) {
		List<String> sensorNoList = new ArrayList<String>();// 传感器编号集合
		List<String> sensorNameList = new ArrayList<String>();// 传感器名称集合
		List<String> sensorValueList = new ArrayList<String>();// 传感器数据集合
		TestDataVo testDataVo = new TestDataVo();
		try {
			BeanUtils.copyProperties(testDataVo, TestData);
			// 切割传感器数据
			String[] split = TestData.getSensorvalue().split("@");
			for (int i = 0; i < split.length; i++) {
				if (!split[i].isEmpty()) {
					String[] sensorInfo = split[i].split(":");
					// 根据传感器编号查询传感器名称
					String sensorName = getSensorNameBySensorNo(TestData.getPrimarykey(), sensorInfo[0]);

					if (sensorName != null) {
						// 进行开停点数据筛选,(如果功率小于5W,则代表停机)
						if (sensorName.equals("功率")) {
							if (Double.parseDouble(sensorInfo[1]) < 5) {
								testDataVo.setStartAndStopStatus("start");
							} else {
								testDataVo.setStartAndStopStatus("stop");
							}
						} else {
							// 对传感器名称和数据进行格式化
							sensorInfo[1] = sensorValueFormat(sensorName, sensorInfo[1]);
							sensorName = sensorNameFormat(sensorName);
							sensorNoList.add(sensorInfo[0]);// 添加传感器编号
							sensorValueList.add(sensorInfo[1]);// 添加传感器数据
							sensorNameList.add(sensorName);// 添加传感器名称
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		testDataVo.setDate(TestData.getPrimarykey().substring(0, 10));
		// 计算出当前数据的时间
		StringBuilder sb = new StringBuilder(TestData.getPrimarykey());
		DateTime dt = new DateTime(sb.insert(10, "T").substring(0, 19));
		double seconds = TestData.getHowlong() * 3600;
		// 四舍五入,保留0位小数
		String formatSeconds = new Formatter().format("%.0f", seconds).toString();
		dt = dt.plusSeconds(Integer.parseInt(formatSeconds));

		String nowDataTime = TestData.getPrimarykey().substring(10, 18) + TestData.getHowlong();
		testDataVo.setTime(dt.toString("HH:mm:ss"));
		testDataVo.setTestunitno(Integer.parseInt(TestData.getPrimarykey().substring(17)));
		testDataVo.setSensorNoList(sensorNoList);
		testDataVo.setSensorValueList(sensorValueList);
		testDataVo.setSensorNameList(sensorNameList);
		return testDataVo;
	}

	/**
	 * 根据primarykey和sensorno查询传感器名称
	 * 
	 * @param primaryKey
	 * @param sensorNo
	 * @return
	 */
	public String getSensorNameBySensorNo(String primaryKey, String sensorNo) {
		SensorInfoModel SensorInfoModel = new SensorInfoModel();
		SensorInfoModel.setPrimarykey(primaryKey);
		SensorInfoModel.setSensorno(Integer.parseInt(sensorNo));
		SensorInfoModel.setSelected(1);// 查找被选择的传感器
		List<SensorInfoModel> sensorInfo = sensorInfoService.getSensorInfoByCondition(SensorInfoModel);
		if (sensorInfo.size() > 0) {
			return sensorInfo.get(0).getName();
		} else {
			return null;
		}
	}

	/**
	 * 对传感器数据进行格式化
	 * 
	 * @param sensorName
	 * @param sensorValue
	 * @return
	 */
	public String sensorValueFormat(String sensorName, String sensorValue) {
		if(sensorValue.equals("N")){
			return "N";
		}
		String sensorValueShow = new String();
		double value = Double.parseDouble(sensorValue);
		if ("电压".equals(sensorName)) {
			// 以四舍五入的方式保留两位小数
			sensorValueShow = new Formatter().format("%.1f", value).toString();
		} else if ("电流".equals(sensorName)) {
			sensorValueShow = new Formatter().format("%.3f", value).toString();
		} else if ("功率".equals(sensorName)) {
			sensorValueShow = new Formatter().format("%.1f", value).toString();
		} else if ("耗电量".equals(sensorName)) {
			sensorValueShow = new Formatter().format("%.4f", value).toString();
		} else if ("频率".equals(sensorName)) {
			sensorValueShow = new Formatter().format("%.1f", value).toString();
		} else {
			sensorValueShow = sensorValue;
		}
		return sensorValueShow;

	}

	/**
	 * 对传感器名称进行格式化
	 * 
	 * @param sensorName
	 * @return
	 */
	public String sensorNameFormat(String sensorName) {
		String sensorNameShow = new String();
		if (sensorName.startsWith("T") || sensorName.startsWith("环温")) {
			sensorNameShow = sensorName + "(℃)";
		} else if ("电压".equals(sensorName)) {
			sensorNameShow = sensorName + "(V)";
		} else if ("电流".equals(sensorName)) {
			sensorNameShow = sensorName + "(A)";
		} else if ("功率".equals(sensorName)) {
			sensorNameShow = sensorName + "(W)";
		} else if ("耗电量".equals(sensorName)) {
			sensorNameShow = sensorName + "(kw.h)";
		} else if ("频率".equals(sensorName)) {
			sensorNameShow = sensorName + "(Hz)";
		} else if ("湿温".equals(sensorName)) {
			sensorNameShow = sensorName + "(℃)";
		} else {
			sensorNameShow = sensorName;
		}
		return sensorNameShow;
	}


	/**
	 * 导出数据到表格
	 * 
	 * @Description:
	 * @author 高杨
	 * @date 2018年9月28日 下午2:43:41
	 * @return List<TestDataVo>
	 */
	public List<TestDataVo> listDatas2Excel(String labcode, int testunitno, String startdate, String enddate) {
		List<TestDataVo> testDataVoList = new ArrayList<TestDataVo>();
		// 根据实验室编码,台位号,日期查询测试数据
		// 1.根据实验室编码,台位号查看台位的开测时间
		TestProdInfoValueModel testProdInfoValueQuery = new TestProdInfoValueModel();
		testProdInfoValueQuery.setLabcode(labcode);
		testProdInfoValueQuery.setTestunitno(testunitno);

		TestProdInfoValueModel testProdInfoValue = testProdInfoValueService
				.getTestProdInfoValueByCondition(testProdInfoValueQuery);
		List<TestDataModel> testDataList = testDataService.getTestData2Excel(testProdInfoValue.getPrimarykey(),
				startdate, enddate);

		// 2.根据台位的开测时间查看测试数据
		for (TestDataModel dataModel : testDataList) {
			TestDataVo testDataVo = TestDataToTestDataVo(dataModel);
			testDataVoList.add(testDataVo);
		}

		// 3.日志打印
		System.out.println("/////////////////////////////////////");
		for (TestDataVo dataVo : testDataVoList) {
			System.out.println(dataVo);
		}
		System.out.println("/////////////////////////////////////");
		return testDataVoList;
	}

	/**
	 * 从redis中查找测试数据(数据列表)
	 * 
	 * @param labcode
	 * @param testunitno
	 * @return
	 */
	public List<TestDataVo> getTestDataFromRedis(String labcode, int testunitno, String startdate, String enddate,
			int pageNum, int pageSize) {
		List<TestDataVo> testDataVoList = new ArrayList<TestDataVo>();
		// 根据实验室编码,台位号,日期查询测试数据
		// 1.根据实验室编码,台位号查看台位的开测时间
		TestProdInfoValueModel testProdInfoValueQuery = new TestProdInfoValueModel();
		testProdInfoValueQuery.setLabcode(labcode);
		testProdInfoValueQuery.setTestunitno(testunitno);
		TestProdInfoValueModel testProdInfoValue = testProdInfoValueService
				.getTestProdInfoValueByCondition(testProdInfoValueQuery);
		PageInfo<TestDataModel> testDataPage = testDataService.getTestDataByCondition(testProdInfoValue.getPrimarykey(),
				startdate, enddate, pageNum, pageSize);
		// 查看最新数据总共有多少页,并设置最新的页数和总记录数
		// 设置分页的相关信息
		total = (int) testDataPage.getTotal();
		pages = testDataPage.getPages();
		// 从缓存中取内容
		String redisKey = "datalist-" + pageNum;
		try {
			// jedisClientSingle.select(1);
			String result = jedisClientSingle.hget(labcode, redisKey);
			if (result != "" && result != null) {
				return JsonUtil.jsonToList(result, TestDataVo.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 2.根据台位的开测时间查看测试数据
		for (int i = 0; i < testDataPage.getList().size(); i++) {
			TestDataVo testDataVo = TestDataToTestDataVo(testDataPage.getList().get(i));
			testDataVoList.add(testDataVo);
		}
		// 向缓存中添加内容
		try {
			jedisClientSingle.hset(labcode, redisKey, JsonUtil.list2json(testDataVoList));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return testDataVoList;
	}

	/**
	 * 从redis中查找开停点数据
	 * 
	 * @param labcode
	 * @param testunitno
	 * @return
	 */
	public List<TestDataVo> getStartAndStopDataFromRedis(String labcode, int testunitno, String startdate,
			String enddate, int pageNum, int pageSize) {
		List<TestDataVo> testDataVoList = new ArrayList<TestDataVo>();
		// 根据实验室编码,台位号,日期查询测试数据
		// 1.根据实验室编码,台位号查看台位的开测时间
		TestProdInfoValueModel testProdInfoValueQuery = new TestProdInfoValueModel();
		testProdInfoValueQuery.setLabcode(labcode);
		testProdInfoValueQuery.setTestunitno(testunitno);
		TestProdInfoValueModel testProdInfoValue = testProdInfoValueService
				.getTestProdInfoValueByCondition(testProdInfoValueQuery);
		PageInfo<TestDataModel> testDataPage = testDataService.getTestDataByCondition(testProdInfoValue.getPrimarykey(),
				startdate, enddate, pageNum, pageSize);
		// 查找开停点数据
		// 查看最新数据总共有多少页,并设置最新的页数和总记录数
		// 设置分页的相关信息
		startAndStopTotal = (int) testDataPage.getTotal();
		startAndStopPages = testDataPage.getPages();
		// 从缓存中取内容
		String redisKey = "startandstop-" + pageNum;
		try {
			// jedisClientSingle.select(1);
			String result = jedisClientSingle.hget(labcode, redisKey);
			if (result != "" && result != null) {
				return JsonUtil.jsonToList(result, TestDataVo.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 2.根据台位的开测时间查看测试数据
		for (int i = 0; i < testDataPage.getList().size(); i++) {
			TestDataVo testDataVo = StartAndStopDataToTestDataVo(testDataPage.getList().get(i));
			// 开停点数据筛选,只对功率进行判断
			testDataVoList.add(testDataVo);
		}
		// 向缓存中添加内容
		try {
			jedisClientSingle.hset(labcode, redisKey, JsonUtil.list2json(testDataVoList));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return testDataVoList;
	}

	/**
	 * 查询最新的10条测试数据(弹出层数据列表专用)
	 */
	public List<TestDataVo> getTestDataNewTop10(String labcode, int testunitno) {
		List<TestDataVo> testDataVoList = new ArrayList<TestDataVo>();
		// 根据实验室编码,台位号,日期查询测试数据
		// 1.根据实验室编码,台位号查看台位的开测时间
		TestProdInfoValueModel testProdInfoValueQuery = new TestProdInfoValueModel();
		testProdInfoValueQuery.setLabcode(labcode);
		testProdInfoValueQuery.setTestunitno(testunitno);
		TestProdInfoValueModel testProdInfoValue = testProdInfoValueService
				.getTestProdInfoValueByCondition(testProdInfoValueQuery);
		// 2.根据开测时间查询最新的10条数据
		List<TestDataModel> testDataNewTop10 = testDataService.getTestDataNewTop10(testProdInfoValue.getPrimarykey());
		// 从缓存中取内容
		try {
			String result = jedisClientSingle.hget(labcode, "testdatanewtop10");
			if (result != "" && result != null) {
				return JsonUtil.jsonToList(result, TestDataVo.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 2.根据台位的开测时间查看测试数据
		for (int i = 0; i < testDataNewTop10.size(); i++) {
			TestDataVo testDataVo = TestDataToTestDataVo(testDataNewTop10.get(i));
			testDataVoList.add(testDataVo);
		}
		// 向缓存中添加内容
		try {
			jedisClientSingle.hset(labcode, "testdatanewtop10", JsonUtil.list2json(testDataVoList));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return testDataVoList;
	}

}
